package khs.java_barcode4j;

import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;

import org.apache.commons.codec.binary.Base64;
import org.krysalis.barcode4j.BarcodeClassResolver;
import org.krysalis.barcode4j.DefaultBarcodeClassResolver;
import org.krysalis.barcode4j.impl.AbstractBarcodeBean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import org.krysalis.barcode4j.tools.MimeTypes;

/**
 * Hello world!
 *
 */
public class App 
{
    private static BufferedReader bufferedReader;
    
    public static void main( String[] args ) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException
    {
        bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        
        String input_mode;
        do
        {
            fnPrintMode();
            input_mode = bufferedReader.readLine();
            
            switch(input_mode)
            {
            case "1":
                fnBarcode2PngMode();
                break;
            case "2":
                fnBarcode2Base64Mode();
            }
        }while(!input_mode.equals("q"));
        
        bufferedReader.close();
    }
    
    private static void fnPrintMode()
    {
        System.out.println("1. barcode2png: ");
        System.out.println("2. barcode2base64");
        System.out.println("q. quit");
    }
    
    private static void fnBarcode2PngMode() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException
    {
        System.out.print("input barcode: ");
        String input_barcode = bufferedReader.readLine();
        
        fnBarcode2Png(input_barcode);
    }
    
    private static void fnBarcode2Png(String input_barcode) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException
    {
        /**
         * 바코드 타입
         * "codabar", "code39", "postnet", "intl2of5", "ean-128"
         * "royal-mail-cbc", "ean-13", "itf-14", "datamatrix", "code128"
         * "pdf417", "upc-a", "upc-e", "usps4cb", "ean-8", "ean-13"
         */
        String barcodeType = "code39";
        final int dpi = 203;
        
        /**
         * SVG, EPS, TIFF, JPEG, PNG, GIF, BMP
         */
        String fileFormat = "png";
        String outputFile = input_barcode + "." + fileFormat;
        
        boolean isAntiAliasing = false;
        
        AbstractBarcodeBean bean = null;
        BarcodeClassResolver resolver = new DefaultBarcodeClassResolver();
        Class clazz = resolver.resolveBean(barcodeType);
        bean = (AbstractBarcodeBean)clazz.newInstance();
        bean.doQuietZone(true);
        
        OutputStream out = new FileOutputStream(new File(outputFile));
        String mimeType = MimeTypes.expandFormat(fileFormat);
        int imageType   = BufferedImage.TYPE_BYTE_BINARY;
        BitmapCanvasProvider canvas = new BitmapCanvasProvider(out, mimeType, dpi, imageType, isAntiAliasing, 0);
        bean.generateBarcode(canvas, input_barcode);
        canvas.finish();
    }
    
    private static void fnBarcode2Base64Mode() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException
    {
        System.out.print("input barcode: ");
        String input_barcode = bufferedReader.readLine();
        
        String base64 = fnBarcode2Base64(input_barcode);
        
        System.out.println(base64);
    }
    
    private static String fnBarcode2Base64(String input_barcode) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException
    {
        /**
         * 바코드 타입
         * "codabar", "code39", "postnet", "intl2of5", "ean-128"
         * "royal-mail-cbc", "ean-13", "itf-14", "datamatrix", "code128"
         * "pdf417", "upc-a", "upc-e", "usps4cb", "ean-8", "ean-13"
         */
        String barcodeType = "code39";
        final int dpi = 203;
        
        /**
         * SVG, EPS, TIFF, JPEG, PNG, GIF, BMP
         */
        String fileFormat = "png";
        String outputFile = input_barcode + "." + fileFormat;
        
        boolean isAntiAliasing = false;
        
        AbstractBarcodeBean bean = null;
        BarcodeClassResolver resolver = new DefaultBarcodeClassResolver();
        Class clazz = resolver.resolveBean(barcodeType);
        bean = (AbstractBarcodeBean)clazz.newInstance();
        bean.doQuietZone(true);
        
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        String mimeType = MimeTypes.expandFormat(fileFormat);
        int imageType   = BufferedImage.TYPE_BYTE_BINARY;
        BitmapCanvasProvider canvas = new BitmapCanvasProvider(out, mimeType, dpi, imageType, isAntiAliasing, 0);
        bean.generateBarcode(canvas, input_barcode);
        canvas.finish();
        
        byte[] imageBytes = out.toByteArray();
        
        String rtn = null;
        rtn = Base64.encodeBase64String(imageBytes);
        
        return rtn;
    }
}
